package com.easywork.mystery.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easycore.utils.BaseController;
import com.easycore.utils.JwtUtils;
import com.easywork.mystery.entity.Contacts;
import com.easywork.mystery.entity.SysUser;
import com.easywork.mystery.rpc.DemoService;
import com.easywork.mystery.service.ContactsService;

@Controller
@RequestMapping("/mona/Contacts")
public class ContactsController extends BaseController {
	@Autowired
	private ContactsService contactsService;
	@Autowired
	private DemoService demoService;

	// -- 找到所有联系人
	@RequestMapping("/findAll")
	@ResponseBody
	public Page<Contacts> findAll(HttpServletRequest req, Integer pageNum, Integer pageSize) {
		String user_s = super.getCookieVal(req, "ssotoken");
		SysUser user = JwtUtils.getObject(user_s, SysUser.class);
		Sort sort = new Sort(Sort.DEFAULT_DIRECTION, "createtime");
		Pageable pageable = new PageRequest(pageNum - 1, pageSize, sort);
		return contactsService.findAll(user, pageable);
	}

	// --显示所有联系人
	@RequestMapping("/showAll")
	@ResponseBody
	public List<Contacts> showAll(HttpServletRequest req) {
		String user_s = super.getCookieVal(req, "ssotoken");
		SysUser user = JwtUtils.getObject(user_s, SysUser.class);
		return contactsService.showAll(user);
	}

	// --路由:跳转添加界面
	@RequestMapping("/toAddCon")
	public String toAddCon() {
		return "/demo/addCon";
	}

	// -- 添加联系人
	@RequestMapping("/addCon")
	@ResponseBody
	public String addCon(HttpServletRequest req, String account) {
		if (account == null || "".equals(account)) {
			return "用户名不可为空";
		}
		String user_s = super.getCookieVal(req, "ssotoken");
		SysUser user = JwtUtils.getObject(user_s, SysUser.class);
		SysUser contact = demoService.getSysUserByAccount(account, user_s);
		if (contact == null) {
			return "无此用户,请确认后再添加!";
		}
		Contacts findOne = contactsService.findOne(user, account);
		if (findOne != null) {
			return "联系人已存在!";
		}
		contactsService.addCon(user, contact, account);
		return "success";
	}

	// -- 删除联系人
	@RequestMapping("/del")
	@ResponseBody
	public String delByConId(Long id) {
		contactsService.delById(id);
		return "success";
	}

	// -- 搜索联系人
	@RequestMapping("/showConByKey")
	@ResponseBody
	public List<Contacts> showConByKey(HttpServletRequest req, String key) {
		String user_s = super.getCookieVal(req, "ssotoken");
		SysUser user = JwtUtils.getObject(user_s, SysUser.class);
		if (key == null || "".equals(key)) {
			return showAll(req);
		}
		return contactsService.findByKey(user, key);
	}
}